import gtk
import sys
import os, shutil, stat
import time, datetime
import LocalView

class Main(LocalView.Main):
	
	def __init__(self, widgetInstance, pathInstance, mimeModule, connectionWidgets, FTPEngine):
		LocalView.Main.__init__(self, widgetInstance, pathInstance, mimeModule)
		self.engine = Engine(mimeModule, FTPEngine)
		self.path = Path(pathInstance, self)
		self.__connectionWidgets = connectionWidgets
	
	def onClick(self, *args):
		treeselection = self.__view.get_selection()
		(tmp_model, tmp_iter) = treeselection.get_selected()
		type = tmp_model.get_value(tmp_iter, 0)
		name = tmp_model.get_value(tmp_iter, 2)
		if type == "directory":
			self.engine.cwd(os.path.join(self.engine.pwd(), name))
			self.populate()
			self.path.update(self.engine.pwd())
		else:
			remotePath, hostname, username, password = \
				self.__path.get_text(), \
				self.__connectionWidgets["hostname"].get_text(), \
				self.__connectionWidgets["username"].get_text(), \
				self.__connectionWidgets["password"].get_text()
				
			down = FTPEngine.Download(name, remotePath, hostname, username, password)
			down.start()
		# What about printing on LogView something? :-|
		#import absynth
		#tryd = absynth.Absynth("")
		#tryd.logViewWidget.write("Something?")

class Engine(LocalView.Engine):
	
	def __init__(self, mimeModule, FTPEngine):
		#print globals()
		LocalView.Engine.__init__(self, mimeModule)
		self.FTPEngine = FTPEngine.FTPConnection()
	
	def pwd(self): return self.FTPEngine.pwd()
	
	def cwd(self, name): self.FTPEngine.cwd(name)
		
	#def delete(self, name): self.__FTPEngine.delete(name)
		
	#def rmdir(self, name): pass
		
	def listFiles(self): return self.FTPEngine.listFiles(self.__mime)
	
class Path(LocalView.Path):

	def __init__(self, widgetInstance, TreeInstance):
		LocalView.Path.__init__(self, widgetInstance, TreeInstance)
		#self.__path = widgetInstance
		self.update("")
		#self.__widget = TreeInstance